/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-09-10
*/
package com.rzico.order.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Product;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.ProductService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.order.entity.*;
import com.rzico.order.service.GroupBuyService;
import com.rzico.order.service.GroupOrderService;
import com.rzico.order.service.GroupPeriodService;
import com.rzico.order.service.GroupProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 团购查询接口
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-09-10
 */
@Api(description = "团购查询接口")
@RestController
@RequestMapping("/groupBuy")
public class GroupBuyController extends BaseController {

    @Autowired
    private GroupBuyService groupBuyService;

    @Autowired
    private GroupProductService groupProductService;

    @Autowired
    private ProductService productService;

    @Autowired
    private GroupOrderService groupOrderService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private GroupPeriodService groupPeriodService;


    /**
     * 分页查询团购管理
     *
     * @return
    */
    @ApiOperation("分页查询团购管理")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mchId", value = "商户号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态（0.待审核，1.已审核, 2.已完成,3.已取消）", dataType = "String", paramType = "query")
    })
    public CommResult<GroupBuy> list(String mchId, Integer status, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

//        if (status!=null){
            params.put("isShow", true);
//        }

        Enterprise enterprise = enterpriseService.findByMch(mchId);
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("无效商户号");
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<GroupBuy> list = groupBuyService.selectList(params);
        for (GroupBuy groupBuy:list) {
//            if (groupBuy.getPeriodId()!=null && groupBuy.getStatus().equals(1)) {
//                GroupPeriod groupPeriod = groupPeriodService.selectByPrimaryKey(groupBuy.getPeriodId());
//                Date c = groupPeriod.getCreateDate();
//                c = DateUtils.addSeconds(c, groupBuy.getDuration());
//                if (c.before(new Date())) {
//                    groupBuy.setStatus(2);
//                }
//            }
            groupBuy.setOrderList(groupOrderService.getItemList(groupBuy.getPeriodId()));
            if (groupBuy.getThumbnail()==null) {
                groupBuy.setItemList(groupProductService.getItemList(groupBuy.getId()));
                for (GroupProduct product:groupBuy.getItemList()) {
                    groupBuy.setThumbnail(product.getThumbnail());
                }
            }
        }
        PageResult<GroupBuy> pageResult = new PageResult<GroupBuy>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条团购管理
     *
     * @return
    */
    @ApiOperation("查询单条团购管理")
    @GetMapping("/find/{id}")
    public CommResult<GroupBuy> find(@PathVariable String id) {

        GroupBuy result = groupBuyService.findById(id);

        result.setItemList(groupProductService.getItemList(result.getId()));


        if (result.getThumbnail()==null) {
            for (GroupProduct product:result.getItemList()) {
                result.setThumbnail(product.getThumbnail());
            }
        }

        if (result.getMeta()==null) {
            for (GroupProduct product:result.getItemList()) {
                Product prod = productService.selectByPrimaryKey(product.getProductId());
                result.setMeta(prod.getMeta());
            }
        }


        result.setOrderList(groupOrderService.getItemList(result.getPeriodId()));

        Map<String,Object> data = new HashMap<>();
        data.put("groupBuy",result);
        return CommResult.success(data);

    }


    /**
     * 查询历史订单
     *
     * @return
     */
    @ApiOperation("查询历史订单")
    @GetMapping("/getOrderList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "团购ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "periodNum", value = "期数", dataType = "String", paramType = "query")
    })
    public CommResult<GroupOrder> getOrderList(Long id,Integer periodNum) {
        Map<String,Object> data = new HashMap<>();
        data.put("orderItem",groupOrderService.getItemList(id,periodNum));
        return CommResult.success(data);

    }

}
